package cn.cvs.controller;

import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.supplier.SupplierService;
import cn.cvs.service.sysRole.SysRoleService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.security.provider.MD2;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 许欣
 * 时间：2022/4/30 14:36
 * 描述：
 */
@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;
    @Resource
    private SysRoleService sysRoleService;
    //查询所有信息
    @RequestMapping(value = "/list")
    public String list(Model model,String supName, String supCode,@RequestParam(defaultValue = "1") Integer currentPageNo) throws Exception {
        //设置分页信息

        List<Supplier>supplierList=null;
        try {
            //设置页面容量
            int pageSize = Constants.pageSize;
            //总数量
            int totalCount = supplierService.getCount(supName, supCode);
            //总页数
            PageSupport pageSupport = new PageSupport();
            pageSupport.setCurrentPageNo(currentPageNo);
            pageSupport.setPageSize(pageSize);
            pageSupport.setTotalCount(totalCount);
            int totalPageCount = pageSupport.getTotalPageCount();
            //控制首页和尾页
            if (currentPageNo > totalPageCount) {
                currentPageNo = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            supplierList = supplierService.getListIndex(supName, supCode, currentPageNo, pageSize);
            model.addAttribute("supplierList", supplierList);
            List<SysRole> roleList = null;
            roleList = sysRoleService.getRoleList();
            //supplierList=supplierService.getList();
            model.addAttribute("roleList", roleList);
            model.addAttribute("supName", supName);
            model.addAttribute("supCode", supCode);
            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", currentPageNo);
        }catch (Exception e){
            e.printStackTrace();
        }

        model.addAttribute("supplierList",supplierService.getList());
        return "/supplier/list";
    }
    //添加
    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "/supplier/add";
    }
    @RequestMapping(value = "/add")
    public String add(Supplier supplier, Model model) throws Exception {
        supplierService.add(supplier);
        return "redirect:/supplier/list";
    }
    //修改
    @RequestMapping(value = "/toUpdate")
    public String toUpdate(@RequestParam Integer id,Model model) throws Exception {
        Supplier supplier=supplierService.getById(id);
        model.addAttribute(supplier);
        return "/supplier/update";
    }
    @RequestMapping(value = "/update")
    public String update(Supplier supplier) throws Exception {
        supplierService.update(supplier);
        return "redirect:/supplier/list";
    }
    //根据id显示详情
    @RequestMapping(value = "/view/{id}")
    public String view(@PathVariable Integer id,Model model) throws Exception {
        Supplier supplier=supplierService.getById(id);
        model.addAttribute(supplier);
        return "/supplier/view";
    }
    //删除
    @RequestMapping(value = "/del/{id}")
    public String del(@PathVariable Integer id) throws Exception {
        supplierService.deleteById(id);
        return "redirect:/supplier/list";
    }
}
